package com.lp.module_lib.network.gson;

import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter;

import java.io.IOException;

/**
 * @ProjectName: Component-master
 * @Package: com.lp.module_lib.network.gson
 * @Description: java类作用描述
 * @CreateDate: 2021/5/12 0012 14:24
 */
public class TypeAdapterBoolean extends TypeAdapter<Boolean> {
    @Override
    public void write(JsonWriter out, Boolean value) throws IOException {
        out.value(value);
    }

    @Override
    public Boolean read(JsonReader in) throws IOException {
        if (in.peek() == JsonToken.NULL) {
            in.nextNull();
            return false;
        } else if (in.peek() == JsonToken.STRING) {
            // support strings for compatibility with GSON 1.7
            String string = in.nextString();
            if ("true".equalsIgnoreCase(string) || "false".equalsIgnoreCase(string))
                return Boolean.parseBoolean(string);
            else
                return !string.equalsIgnoreCase("0");
        } else if (in.peek() == JsonToken.NUMBER) {
            return in.nextInt() > 0;
        }
        return in.nextBoolean();
    }
}
